package com.jpx.jxcupms.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jpx.jxc.common.vo.ResultEntity;
import com.jpx.jxcupms.dto.TUserDto;
import com.jpx.jxcupms.entity.TRole;
import com.jpx.jxcupms.entity.TUser;
import com.jpx.jxcupms.entity.TUserRole;
import com.jpx.jxcupms.service.ITUserRoleService;
import com.jpx.jxcupms.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-22
 */
@RestController
@RequestMapping("/upms/tUser")
public class TUserController {
    @Autowired
    ITUserService itUserService;

    @Autowired
    ITUserRoleService itUserRoleService;

    @RequestMapping("userRoleList")
    public ResultEntity userRoleList(TUser tUser){
        List<TUserDto> tUsers = itUserService.userRoleList(tUser);
        return ResultEntity.ok(tUsers);
    }

    @RequestMapping("/getRoleById")
    public ResultEntity getRoleById(Integer id){
        TUser tUser = itUserService.selectById(id);
        return ResultEntity.ok(tUser);
    }

    @RequestMapping("insertOrUpdateUser")
    public ResultEntity insertOrUpdateUser(TUser tUser){
        System.err.println(tUser);
        itUserService.insertOrUpdate(tUser);
        return ResultEntity.ok();
    }

    @RequestMapping("getRoleByUserid")
    public ResultEntity getRoleByUserid(Integer id){
        List<TRole> list = itUserService.getRoleByUserid(id);
        List<Integer> list1 = new ArrayList<Integer>();
        for (TRole tRole:list) {
            list1.add(tRole.getId());
        }
        return ResultEntity.ok(list1);
    }

    @RequestMapping("delUser")
    @Transactional
    public ResultEntity delUser(Integer id){
        Wrapper<TUserRole> wrapper = new EntityWrapper<TUserRole>();
        wrapper.eq("user_id",id);
        itUserRoleService.delete(wrapper);
        itUserService.deleteById(id);
        return ResultEntity.ok();
    }


}
